// pages/dati/dati.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
      question:'问题??',
      item1:'A、',
      item2:'B、',
      item3:'C、',
      item4:'D、',
      item_show:true,
      img:"",
      img_show:false,
      sub_show:false,
      tip:'',
      group_show:undefined,
      answer:'',
      right:'',
      icon_show:'',
      checked:'',
      show:false,
  },
  
  formSubmit:function(e){
    console.log(e.detail.value);
    if(e.detail.value.select==''){
      return;
    }
    if(this.data.answer=="正确"){
      this.data.answer='A'
    }else if(this.data.answer=="错误"){
      this.data.answer='B'
    }
    var huida=e.detail.value.select;
    var new_huida='';console.log(huida.length);
    for(var i=0;i<huida.length;i++){
      new_huida+=huida[i];
      console.log(i)
    }
   console.log(new_huida)
    if(new_huida!=this.data.answer){
      
        this.setData({
          right:'选'+this.data.answer+',',
          colo:'red',
          icon_show:false,
        })  
        
    }else{
      this.setData({
          right:'',
          icon_show:true,
      })
      setTimeout(()=>{this.next_ti();},700)

    }
    this.setData({
      sub_show:true,
    })
  },
  // 下一题
  next_ti:function () {
    this.setData({
      question:'问题??',
      item1:'A、',
      item2:'B、',
      item3:'C、',
      item4:'D、',
      item_show:true,
      img:"",
      img_show:false,
      sub_show:false,
      tip:'',
      group_show:undefined,
      answer:'',
      right:'',
      icon_show:'',
      checked:'',
      show:false,
    })
   this.req();

  },
  // 预览图片
  previewImg:function(e){
    
    let currentUrl=e.currentTarget.dataset.src;
    wx.previewImage({
      current: currentUrl, // 当前显示图片的http链接
      urls: [currentUrl] // 需要预览的图片http链接列表
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  /*wx.setStorage({key:"subject",data:"2",})
  wx.getStorage({
    key: 'subject',
    success: function(res) {
      var value1 = res.data
      console.log(res);
    },
  })*/
    let pages = getCurrentPages();
    // 获取上一级页面，即pageA的page对象
    let prevPage = pages[pages.length - 2];
    // 获取上一级页面，即pageA的data
    let prevPageData = prevPage.data;
    // 方法1：设置上一级页面，即pageA的data
    prevPage.setData({
        show: true
    });
   this.req();
    
  },
  req:function(){
    var sendDate = (new Date()).getTime();
    wx.request({
      url:"http://localhost/api/huoqu.php",//http://localhost/api/wx/huoqu.php
      data:{
        wx_id:1,
      },
      method:"POST",
      header:{
        "Content-Type":"application/x-www-form-urlencoded",
      },
      success:(z)=>{
        //console.log(z.data);
        if(z.data.item3==''){
          this.setData({
            question:z.data.question,
            item_show:false,
            item1:z.data.item1,
            item2:z.data.item2,
          })
        }else{
          this.setData({
            question:z.data.question,
            item_show:true,
            item1:z.data.item1,
            item2:z.data.item2,
            item3:z.data.item3,
            item4:z.data.item4,
          })
        }
        if(z.data.url!=''){
          this.setData({
            img:z.data.url,
            img_show:true,
          })
        }else{
          this.setData({
            img:'',
            img_show:false,
          })
        }
        // 判断题目类型，判断多选，单选，对错题
        if(z.data.answer=='A'||z.data.answer=='B'||z.data.answer=='C'||z.data.answer=='D'||z.data.answer=='正确'||z.data.answer=='错误'){
          this.setData({
            group_show:true,
            
          })
        }else{
            this.setData({
              group_show:false,
              item_show:false,
            })
        }
        var receiveDate = (new Date()).getTime();
        var time = receiveDate - sendDate;
        this.setData({
          answer:z.data.answer,
          tip:z.data.explains,
        })
        // if(time>1200){
      wx.showToast({
        title: '加载中',
        icon: 'loading',
        duration: time
      })
    // }
    setTimeout(()=>{
      this.setData({
        show:true
      })
    },time)
      },
      fail:function(){
        console.log('请求失败');
      },
      complete:function(){
        //console.log('不管成功还是失败都会执行')
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
})